import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class CreateDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textVexs;
	private JTextField textArcs;
	private JComboBox<Object> comboBoxKinds;
	private MainFrame mainFrame;

	/**
	 * Create the dialog.
	 */
	public CreateDialog(MainFrame mainframe) {
		super(mainframe, ModalityType.APPLICATION_MODAL);
		this.mainFrame = mainframe;
		setTitle("Create Graph");
		setResizable(false);
		setBounds(mainframe.getX(), mainframe.getY(), 384, 191);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			comboBoxKinds = new JComboBox<Object>(GraphKind.values());
			comboBoxKinds.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					comboBoxKindsChangedCall();
				}
			});
			comboBoxKinds.setBounds(121, 13, 243, 24);
			contentPanel.add(comboBoxKinds);
		}
		{
			textVexs = new JTextField();
			textVexs.setText("0;1;2;3;4");
			textVexs.setBounds(57, 50, 307, 24);
			contentPanel.add(textVexs);
			textVexs.setColumns(10);
		}
		{
			JLabel lblVexs = new JLabel("Vexs:");
			lblVexs.setBounds(14, 53, 40, 18);
			contentPanel.add(lblVexs);
		}
		{
			JLabel lblArcs = new JLabel("Arcs:");
			lblArcs.setBounds(14, 90, 40, 18);
			contentPanel.add(lblArcs);
		}
		{
			textArcs = new JTextField();
			textArcs.setText("0,1;1,2;1,4;2,3;3,4");
			textArcs.setColumns(10);
			textArcs.setBounds(57, 87, 307, 24);
			contentPanel.add(textArcs);
		}
		{
			JLabel lblGraphKind = new JLabel("Graph Kind:");
			lblGraphKind.setBounds(14, 16, 93, 18);
			contentPanel.add(lblGraphKind);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Create");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okButtonCall();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
	}

	protected void comboBoxKindsChangedCall() {
		switch ((GraphKind) comboBoxKinds.getSelectedItem()) {
		case UDG:
			textArcs.setText("0,1;1,2;1,4;2,3;3,4");
			textVexs.setText("0;1;2;3;4");
			break;
		case DG:
			textArcs.setText("0,1;0,2;2,3;3,0;3,4");
			textVexs.setText("V0;V1;V2;V3;V4");
			break;
		case UDN:
			textArcs.setText("0,2,2;0,1,10;1,3,7;1,5,5;3,4,2;4,5,5;");
			textVexs.setText("A;B;C;D;E;F");
			break;
		case DN:
			textArcs.setText("0,1,1;0,2,4;2,3,7;3,2,4;3,0,6");
			textVexs.setText("A;B;C;D");
			break;
		default:
			textArcs.setText("");
			textVexs.setText("");
			break;
		}
	}

	protected void okButtonCall() {
		if (textVexs.getText().length() < 1) {
			JOptionPane.showMessageDialog(this, "Input the vexs!", "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (textArcs.getText().length() < 1) {
			JOptionPane.showMessageDialog(this, "Input the arcs!", "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		int[][] arcs = null;
		try {
			switch ((GraphKind) comboBoxKinds.getSelectedItem()) {
			case UDG:
			case DG:
				arcs = resolveDG(textArcs.getText());
				break;
			case UDN:
			case DN:
				arcs = resolveDN(textArcs.getText());
				break;
			}
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		} catch (ArrayIndexOutOfBoundsException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		String vexs[] = textVexs.getText().split(";");
		try {
			MGraph graph = new MGraph(
					(GraphKind) comboBoxKinds.getSelectedItem(), vexs, arcs);
			mainFrame.addGraph(graph);
		} catch (ArrayIndexOutOfBoundsException e) {
			JOptionPane.showMessageDialog(this, "Error input!", "Error",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		this.dispose();
	}

	private int[][] resolveDG(String str)
			throws NumberFormatException, ArrayIndexOutOfBoundsException {
		String[] arcs = str.split(";");
		int[][] result = new int[arcs.length][2];
		for (int i = 0; i < arcs.length; i++) {
			String[] arc = arcs[i].split(",");
			result[i][0] = Integer.parseInt(arc[0]);
			result[i][1] = Integer.parseInt(arc[1]);
		}
		return result;
	}

	private int[][] resolveDN(String str)
			throws NumberFormatException, ArrayIndexOutOfBoundsException {
		String[] arcs = str.split(";");
		int[][] result = new int[arcs.length][3];
		for (int i = 0; i < arcs.length; i++) {
			String[] arc = arcs[i].split(",");
			result[i][0] = Integer.parseInt(arc[0]);
			result[i][1] = Integer.parseInt(arc[1]);
			result[i][2] = Integer.parseInt(arc[2]);
		}
		return result;
	}

}
